# -*- coding: utf-8 -*-
from .base import Base

from aiosqlite import Connection


class Region(Base("regions", ("name", "parentRegion", "regionType", "latitude", "longitude", "accountId"))):
    id:           int
    name:         str
    parentRegion: str
    regionType:   int
    latitude:     float
    longitude:    float
    accountId:    int

    @classmethod
    async def get_by_coordinates(cls, latitude: float, longitude: float, database: Connection) -> "Region":
        cursor = await database.execute(f"SELECT * FROM regions WHERE latitude = '{latitude}' AND longitude = '{longitude}'")
        region = await cls.parse(cls, cursor)

        if region == None:
            return None

        region.database = database
        return region
